const app = getApp()
const mtm_utils=require("../common/utils/mtm_utils.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custSourceListData: app.globalData.custSourceListData,
    custStatuListData: app.globalData.custStatuListData,
    yearListData: app.globalData.yearListData,
    mileageListData: app.globalData.mileageListData,
    colorListData: app.globalData.colorListData,
    cityListData: app.globalData.cityListData,
    storeListData:app.globalData.storeListData,
    request_common_para: app.globalData.request_common_para,
    messageList: [],
    index: 0,
    chosen: '',
    car_model: "+",
    carType: 0,
    id: '',
    cust_tel: '',
    updateDateFormat: '',
    cust_list: [],
    isYearShow:false,
    initYearVal:"不限",
    isMileageShow:false,
    initMileageVal:"不限",
    isCarColorShow:false,
    initCarColorVal:"其他",
    isCarSales:false

  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.intiSysConfigData()
    let store_name = app.globalData.request_common_para.store_name;
    let storeListData= app.globalData.storeListData
    let index = storeListData.indexOf(store_name) == -1 ? 0 : storeListData.indexOf(store_name)
    this.setData({
      custSourceListData: app.globalData.custSourceListData,
      storeListData: storeListData,
      index: index
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 表单提交事件
   */
  submitCustInfo: function(e) {
    let form_id = e.detail.formId
    try{
      if (form_id){
        app.saveFormId(form_id)
      }
    }catch(e){
      console.info("app.saveFormId err"+e);
    }

    let req_data = this.data.request_common_para;
    let city = this.data.cityListData[this.data.index]
    let store_name=this.data.storeListData[this.data.index]
    
    let cust = e.detail.value
    let id = cust.id
    let cust_name = cust.cust_name //客户姓名
    if (!cust_name) {
      app.noTitlemodalTap("请输入客户姓名")
      return;
    }
    var cust_tel = cust.cust_tel //客户电话
    if (!cust_tel) {
      app.noTitlemodalTap("请输入客户手机")
      return;
    }
    if (cust_tel!=""){
      cust_tel = cust_tel.replace(/ /g,"").replace("-","")
    }
    // if (!mtm_utils.isValidMobile(cust_tel)) {
    //   app.noTitlemodalTap("该号码不合法")
    //   return;
    // }

    var car_source = cust.car_source //客户来源
    if (!car_source) {
      app.noTitlemodalTap("请选择客户来源")
      return;
    }

    var cust_status = cust.cust_status //购车意向
    if (!cust_status) {
      app.noTitlemodalTap("请选择意向等级")
      return;
    }
    var begin_price = cust.begin_price //价格
    var end_price = cust.end_price
    if (!begin_price || !end_price) {
      app.noTitlemodalTap("请输入购车预算")
      return;
    }
    if (end_price * 1 < begin_price * 1) {
      app.noTitlemodalTap("请输入正确的购车预算")
      return;
    }
    let car_model = cust.car_model.split(',');
    car_model = car_model.join(',');
    // let car_model1 = cust.car_model1 //车型 
    // let car_model2 = cust.car_model2;
    // if (car_model1 != '+') {
    //   car_model = car_model1
    // }
    // if (car_model2 != '+') {
    //   car_model = car_model2
    // }
    // if (car_model1 != '+' && car_model2 != '+') {
    //   car_model = car_model1 + "," + car_model2;
    // }
    if (car_model=='' || car_model =='+') {
      app.noTitlemodalTap("请选择车型")
      return;
    }
    let vinCode = cust.vinCode
    if (this.data.isCarSales){
      if (!vinCode){
        app.noTitlemodalTap("6位V码不能为空")
        return;
      }
    }

    var car_year_val = "";
    /**
    var car_year_list = cust.year_list //年限
    
    for (var i in car_year_list) {
      car_year_val += car_year_list[i]
      if (i != car_year_list.length - 1) {
        car_year_val += "|"
      }
    }
    if (!car_year_val) {
      app.noTitlemodalTap("请选择年限要求")
      return;
    }
    * */
    car_year_val=this.data.initYearVal
    var car_mileage_val =this.data.initMileageVal
    /**
    var car_mileage_list = cust.mileage_list //里程
    
    for (var i in car_mileage_list) {
      car_mileage_val += car_mileage_list[i];
      if (i != car_mileage_list.length - 1) {
        car_mileage_val += "|"
      }
    }
    if (!car_mileage_val) {
      app.noTitlemodalTap("请选择里程要求")
      return;
    } */

    var car_color_val = this.data.initCarColorVal
    /**
    var car_color_list = cust.car_color //颜色
    for (var i in car_color_list) {
      car_color_val += car_color_list[i]
      if (i != car_color_list.length - 1) {
        car_color_val += "|"
      }
    }
    if (!car_color_val) {
      app.noTitlemodalTap("请选择颜色要求")
      return;
    }
  */
    var car_no_region = cust.car_no_region //落地户
    var note = cust.note //备注
    var content = content
    var other_car_no_region = cust.other_car_no_region //外地车牌
    /**
    if (!car_no_region) {
      app.noTitlemodalTap("请选择落地户")
      return;
    }
    */
    if (car_no_region == "外地") {
      if (!other_car_no_region) {
        app.noTitlemodalTap("请输入外地车牌")
        return;
      }
      car_no_region = other_car_no_region
    }
    app.toast3Tap()
    req_data.id = id
    req_data.audit = 0
    req_data.budgetLimitMax = end_price
    req_data.budgetLimitMin = begin_price
    req_data.carModel = car_model
    req_data.vin_code = vinCode
    req_data.city = city
    req_data.store_name=store_name
    req_data.content = content
    req_data.count = 0
    req_data.customerName = cust_name
    req_data.customerPhone = cust_tel
    req_data.customerSource = car_source
    req_data.note = note
    req_data.requirementColor = car_color_val
    req_data.requirementMileage = car_mileage_val
    req_data.requirementSettled_in = car_no_region
    req_data.requirementYear = car_year_val
    req_data.state_name = cust_status
    req_data.sumParam = 0
    req_data.form_id = form_id
    if (id == '') {
      console.info("***********************客户信息新增***********************************")
      wx.request({
        url: app.getHost()+'/datalake/v1/userManages',
        data: req_data,
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: res => {
          if (res.statusCode == 200) {
            app.toast1Tap()
            console.info("创建客户：" + JSON.stringify(res.data))
            wx.reLaunch({
              url: 'custInfoList',
            })
            this.setData({
              id: res.data.id,
              cust_tel: res.data.customerPhone,
              car_model: res.data.carModel,
              updateDateFormat: res.data.updateDateFormat
            })
           /**
            //查询消息
            let msg_req_data = {}
            msg_req_data.userInfo = this.data.request_common_para
            msg_req_data.customerPhone = this.data.cust_tel
            msg_req_data.carModel = [this.data.car_model]
            console.info("https://mcc.mtmche.cn/datalake/v1/userManages/messages request params:" + JSON.stringify(msg_req_data))
            wx.request({
              url: 'https://mcc.mtmche.cn/datalake/v1/userManages/messages',
              data: msg_req_data,
              method: "POST",
              success: res => {
                console.info("https://mcc.mtmche.cn/datalake/v1/userManages/messages result value" + JSON.stringify(res))
                this.setData({
                  messageList: res.data
                })
              },
              fail: function(res) {},
              complete: function(res) {}
            })
         */
          }
        },
        complete: function(res) {},
        fail: function(res) {
          console.error(res);
        }
      })
    }
 
    /**
    if (this.data.id != '') {
      //查询消息
      let msg_req_data = {}
      msg_req_data.userInfo = this.data.request_common_para
      msg_req_data.customerPhone = this.data.cust_tel
      msg_req_data.carModel = [this.data.car_model]
      console.info("https://mcc.mtmche.cn/datalake/v1/userManages/messages request params:" + JSON.stringify(msg_req_data))
      wx.request({
        url: 'https://mcc.mtmche.cn/datalake/v1/userManages/messages',
        data: msg_req_data,
        method: "POST",
        success: res => {
          console.info("https://mcc.mtmche.cn/datalake/v1/userManages/messages result value" + JSON.stringify(res))
          console.info(res.data)
          this.setData({
            messageList: res.data
          })
        },
        fail: function(res) {},
        complete: function(res) {}
      })

      wx.request({
        url: 'https://mcc.mtmche.cn/datalake/v1/userManages/' + id,
        data: req_data,
        method: "PUT",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function(res) {
          if (res.statusCode == 200) {
            app.toast1Tap()
            console.info("修改客户：" + JSON.stringify(res.data))
          }
        },
        complete: function(res) {},
        fail: function(res) {
          console.error(res);
        }
      })
    }
 */


  },
  bindinputCarModel:function(e){
    wx.navigateTo({
      url: 'carModelType?selectedCarModelType=' + this.data.car_model + '&carType=' + this.data.carType
    })
  },
  userTypeUrl: function(e) {
    var id = e.currentTarget.id
    wx.navigateTo({
      url: '../out/out?html_src=' + id,
    })
  },
  validTelBindblur: function(e) {
    var tel = e.detail.value
    
    var that = this
    wx.request({
      url: app.getHost()+'/datalake/v1/userManages?Customerphone=' + tel,
      method: "GET",
      success: function(res) {
        if (res.data.length > 0) {
          app.noTitlemodalTap("该号码已存，不允许新建记录")
          that.setData({
            cust_tel: ''
          })
        }
      }
    })
  },
  bindtapisYearShow:function(e){
    if(this.data.isYearShow){
      this.setData({ isYearShow:false})
    }else{
      this.setData({ isYearShow: true })
    }
  },
  bindchangeYear:function(e){
    let year_check_list=e.detail.value;
    let val="";
    for (let i in year_check_list){
      val += year_check_list[i]
      if (i != year_check_list.length-1){
        val+="|"
      }
    }
    if (val==""){
      val="不限"
    }
    this.setData({ initYearVal: val })
  },
  bindtapisMileageShow: function (e) {
    if (this.data.isMileageShow) {
      this.setData({ isMileageShow: false })
    } else {
      this.setData({ isMileageShow: true })
    }
  },
  bindchangeMileage: function (e) {
    let mileage_list = e.detail.value;
    let val = "";
    for (let i in mileage_list) {
      val += mileage_list[i]
      if (i != mileage_list.length - 1) {
        val += "|"
      }
    }
    if (val == "") {
      val = "不限"
    }
    this.setData({ initMileageVal: val })
  },
  bindtapisCarColorShow: function (e) {
    if (this.data.isCarColorShow) {
      this.setData({ isCarColorShow: false })
    } else {
      this.setData({ isCarColorShow: true })
    }
  },
  bindchangeCarColor: function (e) {
    let car_color_list = e.detail.value;
    let val = "";
    for (let i in car_color_list) {
      val += car_color_list[i]
      if (i != car_color_list.length - 1) {
        val += "|"
      }
    }
    if (val == "") {
      val = "不限"
    }
    this.setData({ initCarColorVal: val })
  },
  isCarSalesBindtap:function(e){
    this.setData({
      isCarSales: this.data.isCarSales ?false:true 
    })
  }
})